# -*- coding:utf-8 -*-

import os

from src.package_helper.pkg_helper import PkgHelperInterface
from src.utils import runcmd
from src.core import macro
from src.utils.logger import logger

RPM_NAMES = "openEuler-logos"
SPEC_NAMES = "openEuler-logos.spec"


class PkgLogos(PkgHelperInterface):
    _vendor_src = None
    _logos_src = None

    def __init__(self, vendor_src, logos_src):
        self._vendor_src = vendor_src
        self._logos_src = logos_src
        self._spec_names = SPEC_NAMES
        runcmd.rpm_src_setup(vendor_src + "/" + RPM_NAMES + "*")
        runcmd.rpm_src_build_pre(macro.RPM_SRC_SETUP_SPEC_PATH + self._spec_names)

    def replace_vendor(self):
        self._replace_spec()
        self._replace_logo_file()

    def _replace_spec(self):
        logger.warning("begin replace logos vendor")
        spec = macro.RPM_SRC_SETUP_SPEC_PATH + self._spec_names
        runcmd.replace(spec, "openEuler", "%{product_name}")
        runcmd.replace(spec,
                       "Provides:",
                       "Provides:       openEuler-logos = %{version}-%{release}\nProvides:", 1)
        data = runcmd.read_file(spec)
        data = macro.ADDTION_HEAD_LINE + data
        runcmd.rewrite_file(spec, data)

    def _replace_logo_file(self):
        dst_path = macro.RPM_SRC_SETUP_SOURCE_PATH
        runcmd.cp_catalog(self._logos_src, dst_path)

    def replace_rpm(self):
        pass
